package com.hyts.bmsp.dao.account;


import com.hyts.bmsp.model.account.*;
import com.hyts.bmsp.tool.EncryptUtil;
import com.hyts.bmsp.tool.JFinalUtil;

/**
 * @ClassName: LoginDAO
 * @Description: 登录操作数据访问层
 * @author: 李博/Alex
 * @date: 2017-2-16 下午08:08:05
 */
public class UserInfoDAO {

	
	/**
	 * @Title: queryPasswordByUserName
	 * @Description: 查询密码根据用户名(tbl_user_info)
	 * @param userName
	 * @return
	 * @return: String
	 */
	public LoginReturnBO queryPasswordByUserName(String userName){
		 
		 AccountInfoBean bean = AccountInfoBean.userDAO.findFirst("SELECT USER_ID,PASSWORD FROM TBL_ACCOUNT_INFO WHERE ACCOUNT_NAME=?",userName);
		 LoginReturnBO loginBO = new LoginReturnBO();
		 if(bean != null){
			 loginBO.password = bean.get("PASSWORD");
			 loginBO.userId = bean.get("USER_ID");
		 }
		 return loginBO;
	}
	
	/**
	 * @Title: queryAccountDataByUserId
	 * @Description: 查询账户基本信息操作，根据用户ID
	 * @param userId
	 * @return
	 * @return: UserInfoBean
	 */
	public AccountInfoBean queryAccountDataByUserId(String userId){
		String sql = "SELECT ID,ACCOUNT_NAME,PASSWORD,USER_ID,ENCRYPT_PASSWORD,ROLE_ID,STATUS,CREATE_DATE FROM TBL_ACCOUNT_INFO WHERE USER_ID=?";
		AccountInfoBean bean = AccountInfoBean.userDAO.findFirst(sql, userId);
		return bean;
	}
	
	
	/**
	 * @Title: editAccountPassword
	 * @Description: 更新修改密码操作
	 * @param dto
	 * @return
	 * @return: Boolean
	 */
	public Boolean editAccountPassword(ChangePassWordDTO dto){
		AccountInfoBean bean = AccountInfoBean.userDAO.findFirst("SELECT id,ACCOUNT_NAME,PASSWORD,USER_ID,ENCRYPT_PASSWORD,ROLE_ID,STATUS,CREATE_DATE FROM TBL_ACCOUNT_INFO WHERE USER_ID=?",dto.userId);
		return bean.set("PASSWORD",dto.newPassword).set("ENCRYPT_PASSWORD", EncryptUtil.md5(dto.newPassword)).update();
	}
	
	
	/**
	 * @Title: loadUserInfo
	 * @Description: 加载用户信息数据
	 * @param userId
	 * @return
	 * @return: UserInfoBean
	 */
	public UserInfoBean loadUserInfo(String userId){
		UserInfoBean bean = UserInfoBean.userDAO.findFirst("SELECT id,REAL_NAME,ALIAS_NAME,EMAIL,QQ,WEI_CHAT,TELEPHONE,REMARK FROM TBL_USER_INFO WHERE ID = ?", userId);
		return bean;
	}
	
	/**
	 * @Title: editUserInfo
	 * @Description: 加载用户信息数据
	 * @param userId
	 * @return 
	 * @return: Boolean
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public Boolean editUserInfo(String userId,LoadUserInfoDTO dto) throws IllegalArgumentException, IllegalAccessException{
		UserInfoBean bean = UserInfoBean.userDAO.findFirst("SELECT id,REAL_NAME,ALIAS_NAME,EMAIL,QQ,WEI_CHAT,TELEPHONE,REMARK FROM TBL_USER_INFO WHERE ID = ?", userId);
		return JFinalUtil.daoUpdateSetHandler(bean, dto).set("ID",userId).update();
	}
}
